package com.computergames.server.util;

import org.yaml.snakeyaml.Yaml;

import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 读取游戏房间配置文件
 */

public class GameRoomConfigLoader {
    private static final Logger LOGGER = Logger.getLogger(GameRoomConfigLoader.class.getName());
    private static final String CONFIG_FILE_PATH = "D:\\try\\ComputerGamesServer\\ComputerGamesServer\\src\\main\\resources\\game_room_config.yml";

    public static int getMaxPlayersForRoomType(String roomType) {
        try (InputStream inputStream = new FileInputStream("D:\\try\\ComputerGamesServer\\ComputerGamesServer\\src\\main\\resources\\game_room_config.yml")) {
            Yaml yaml = new Yaml();
            Map<String, Object> data = yaml.load(inputStream);

            @SuppressWarnings("unchecked")
            List<Map<String, Object>> rooms = (List<Map<String, Object>>) data.get("rooms");

            int maxPlayers = rooms.stream()
                    .filter(room -> room.containsKey("type") && room.get("type").equals(roomType))
                    .mapToInt(room -> {
                        try {
                            return Integer.parseInt(room.get("max_players").toString());
                        } catch (NumberFormatException e) {
                            throw new IllegalArgumentException("Invalid max_players value for room type: " + roomType, e);
                        }
                    })
                    .findFirst()
                    .orElseThrow(() -> new IllegalArgumentException("Unknown room type: " + roomType));

            return maxPlayers;
        } catch (IOException e) {
            throw new RuntimeException("Failed to load game room config file", e);
        }
    }
}
